﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using Serialization;

namespace EDMD
{
    public class i7s : DataService<i7e>
    {

        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);            
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.AcceptCountRequests = true;     
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;            
            //config.SetEntitySetPageSize("CalendarItem_Field", 5000);
        }
        
        [WebGet]
        public byte[] i_GetCalendarItems_in_Binary_via_CustomFunction()
        {
            ObservableCollection<i_CalendarItem> NewCalendarItems = new ObservableCollection<i_CalendarItem>();
            i7e context = this.CurrentDataSource;
            foreach (var CurCalendarItem in context.CalendarItems)
                NewCalendarItems.Add(s_CalendarItem.Convert(CurCalendarItem));
            byte[] ResultBinary = SilverlightSerializer.Serialize(NewCalendarItems);
            return ResultBinary;
        }//public byte[] GetCalendarItems_in_Binary__via_CustomFunction()


        [WebGet]
        public byte[] i_GetCalendarItemFields_in_Binary_via_CustomFunction()
        {
            ObservableCollection<i_CalendarItem_Field> NewCalendarItem_Fields = new ObservableCollection<i_CalendarItem_Field>();
            i7e context = this.CurrentDataSource;
            foreach (var CurCalendarItem_Field in context.CalendarItem_Field)
                NewCalendarItem_Fields.Add(s_CalendarItem_Field.Convert(CurCalendarItem_Field));
            byte[] ResultBinary = SilverlightSerializer.Serialize(NewCalendarItem_Fields);
            return ResultBinary;
        }//public byte[] GetCalendarItem_Fields_in_Binary__via_CustomFunction()

        [WebGet]
        public byte[] i_Getvw_CalendarItem_Notes_in_Binary_via_CustomFunction()
        {
            ObservableCollection<i_vw_CalendarItem_Note> Newvw_CalendarItem_Notes = new ObservableCollection<i_vw_CalendarItem_Note>();
            i7e context = this.CurrentDataSource;
            foreach (var Curvw_CalendarItem_Note in context.vw_CalendarItem_Note)
                Newvw_CalendarItem_Notes.Add(s_vw_CalendarItem_Note.Convert(Curvw_CalendarItem_Note));            
            byte[] ResultBinary = SilverlightSerializer.Serialize(Newvw_CalendarItem_Notes);
            return ResultBinary;
        }//public byte[] Getvw_CalendarItem_Notes_in_Binary__via_CustomFunction()

        [WebGet]
        public byte[] i_GetMUTLI_TABLES__in_Binary_via_CustomFunction()
        {
            
            i7e context = this.CurrentDataSource;
            ObservableCollection<object> MUTLI_TABLES = new ObservableCollection<object>();
            ObservableCollection<i_Entry> NewEntrys = new ObservableCollection<i_Entry>();
            foreach (var CurEntry in context.Entries)
                NewEntrys.Add(s_Entry.Convert(CurEntry));
            MUTLI_TABLES.Add(NewEntrys);

            ObservableCollection<i_Entry_Field> NewEntry_Fields = new ObservableCollection<i_Entry_Field>();
            foreach (var CurEntry_Field in context.Entry_Field)
                NewEntry_Fields.Add(s_Entry_Field.Convert(CurEntry_Field));
            MUTLI_TABLES.Add(NewEntry_Fields);

            ObservableCollection<i_Field> NewFields = new ObservableCollection<i_Field>();
            foreach (var CurField in context.Fields)
                NewFields.Add(s_Field.Convert(CurField));
            MUTLI_TABLES.Add(NewFields);

            ObservableCollection<i_Field_Details> NewField_Details = new ObservableCollection<i_Field_Details>();
            foreach (var CurField_Details in context.Field_Details)
                NewField_Details.Add(s_Field_Details.Convert(CurField_Details));
            MUTLI_TABLES.Add(NewField_Details);


            ObservableCollection<i_Field_Type> NewField_Type = new ObservableCollection<i_Field_Type>();
            foreach (var CurField_Type in context.Field_Type)
                NewField_Type.Add(s_Field_Type.Convert(CurField_Type));
            MUTLI_TABLES.Add(NewField_Type);

            ObservableCollection<i_Verb> NewVerb = new ObservableCollection<i_Verb>();
            foreach (var CurVerb in context.Verbs)
                NewVerb.Add(s_Verb.Convert(CurVerb));
            MUTLI_TABLES.Add(NewVerb);

            ObservableCollection<i_Status> NewStatus = new ObservableCollection<i_Status>();
            foreach (var CurStatus in context.Status)
                NewStatus.Add(s_Status.Convert(CurStatus));
            MUTLI_TABLES.Add(NewStatus);

            ObservableCollection<i_CalendarItem_Related> NewCalendarItem_Related = new ObservableCollection<i_CalendarItem_Related>();
            foreach (var CurCalendarItem_Related in context.CalendarItem_Related)
                NewCalendarItem_Related.Add(s_CalendarItem_Related.Convert(CurCalendarItem_Related));
            MUTLI_TABLES.Add(NewCalendarItem_Related);

            ObservableCollection<i_CalendarItemRecurrence> NewCalendarItemRecurrence = new ObservableCollection<i_CalendarItemRecurrence>();
            foreach (var CurCalendarItemRecurrence in context.CalendarItemRecurrences)
                NewCalendarItemRecurrence.Add(s_CalendarItemRecurrence.Convert(CurCalendarItemRecurrence));
            MUTLI_TABLES.Add(NewCalendarItemRecurrence);

            ObservableCollection<i_Calendar> NewCalendar = new ObservableCollection<i_Calendar>();
            foreach (var CurCalendar in context.Calendars)
                NewCalendar.Add(s_Calendar.Convert(CurCalendar));
            MUTLI_TABLES.Add(NewCalendar);
                       

            byte[] ResultBinary = SilverlightSerializer.Serialize(MUTLI_TABLES);
            return ResultBinary;
        }//public byte[] i_GetMUTLI_TABLES__in_Binary_via_CustomFunction()
        
        [WebGet]
        public int GetCountOfRecordsOfAllTables()
        {
            int GetCountOfRecordsOfAllTables = -1;
            i7e context = this.CurrentDataSource;
            if (context != null)
                GetCountOfRecordsOfAllTables =
                    context.CalendarItems.Count() +
                    context.Calendars.Count() +
                    /*context.vw_Appointment_Summary.Count() +*/
                    context.CalendarItem_Field.Count() +
                    context.vw_CalendarItem_Note.Count() +
                    context.Entries.Count() +
                    context.Entry_Field.Count() +
                    context.Fields.Count() +
                    context.Field_Details.Count() +
                    context.Field_Type.Count() +
                    context.Verbs.Count() +
                    context.Status.Count() +
                    context.CalendarItem_Related.Count() +
                    context.CalendarItemRecurrences.Count();
            return GetCountOfRecordsOfAllTables;
        }//public int GetCountOfRecordsOfAllTables()

        [WebGet]
        public byte[] SearchCalendarItems_in_Binary_via_CustomFunction(
            string SerachString)
        {
            ObservableCollection<i_CalendarItem> NewCalendarItems = new ObservableCollection<i_CalendarItem>();
            i7e context = this.CurrentDataSource;
            //.Contains(SerachString)
            foreach (var CurCalendarItem in context.CalendarItems)
              if (CurCalendarItem.FirstName != null && CurCalendarItem.LastName != null)
                  if (CurCalendarItem.FirstName.TrimEnd().Contains(SerachString) |
                    CurCalendarItem.LastName.TrimEnd().Contains(SerachString)
                      )
                          NewCalendarItems.Add(s_CalendarItem.Convert(CurCalendarItem));
            byte[] ResultBinary = SilverlightSerializer.Serialize(NewCalendarItems);
            return ResultBinary;
        }//public byte[] SearchCalendarItems_in_Binary_via_CustomFunction(string FirstNameToSerach, string LastNameToSerach)
                
        //[WebInvoke(UriTemplate = "i_Getvw_Appointment_Detail_in_Binary_via_CustomFunction", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
        [WebGet]
        public byte[] i_Getvw_Appointment_Detail_in_Binary_via_CustomFunction(int CalendarItem_ID_Param, int Entry_ID_Param, string NewCalendarItemInBase64)
        {
            i7e context = this.CurrentDataSource;
            CalendarItem NewCalendarItem = null;
            if (!string.IsNullOrEmpty(NewCalendarItemInBase64))
            {   
                i_CalendarItem TempCalendarItem = (i_CalendarItem)SilverlightSerializer.Deserialize(Convert.FromBase64String(NewCalendarItemInBase64));
                NewCalendarItem = new CalendarItem()
                     {
                        CalendarItem_ID = TempCalendarItem.CalendarItem_ID,
                        CalendarItem_Calendar_ID = Utils.ReverseIsNull(TempCalendarItem.CalendarItem_Calendar_ID),
                        Verb_ID = Utils.ReverseIsNull(TempCalendarItem.Verb_ID),
                        Entry_ID = Entry_ID_Param == -1 ? Utils.ReverseIsNull(TempCalendarItem.Entry_ID) : Entry_ID_Param,
                        Entry_Name = TempCalendarItem.Entry_Name,
                        Status_ID = Utils.ReverseIsNull(TempCalendarItem.Status_ID),
                        StatusNote = TempCalendarItem.StatusNote,
                        isNoDate = Utils.ReverseIsNull(TempCalendarItem.isNoDate),
                        isAllDay = Utils.ReverseIsNull(TempCalendarItem.isAllDay),
                        isNote = Utils.ReverseIsNull(TempCalendarItem.isNote),
                        isRollover = Utils.ReverseIsNull(TempCalendarItem.isRollover),
                        isSpan = Utils.ReverseIsNull(TempCalendarItem.isSpan),
                        isEmailConfirmRequired = Utils.ReverseIsNull(TempCalendarItem.isEmailConfirmRequired),
                        Start_Date = Utils.ReverseIsNull(TempCalendarItem.Start_Date),
                        End_Date = Utils.ReverseIsNull(TempCalendarItem.End_Date),
                        Create_Date = Utils.ReverseIsNull(TempCalendarItem.Create_Date),
                        Update_Date = Utils.ReverseIsNull(TempCalendarItem.Update_Date),
                        Created_User_ID = Utils.ReverseIsNull(TempCalendarItem.Created_User_ID),
                        Updated_User_ID = Utils.ReverseIsNull(TempCalendarItem.Updated_User_ID),
                        GUID = TempCalendarItem.GUID,
                        Salutation = Utils.ReverseIsNull(TempCalendarItem.Salutation),
                        FirstName = Utils.ReverseIsNull(TempCalendarItem.FirstName),
                        MiddleName = Utils.ReverseIsNull(TempCalendarItem.MiddleName),
                        LastName = Utils.ReverseIsNull(TempCalendarItem.LastName)
                    };
            }
            else
            {
                var TempCalendarItems = from CalendarItemsOO in context.CalendarItems
                                        where (CalendarItemsOO.CalendarItem_ID == CalendarItem_ID_Param)
                                        select new
                                        {
                                            CalendarItem_ID = CalendarItemsOO.CalendarItem_ID,
                                            CalendarItem_Calendar_ID = CalendarItemsOO.CalendarItem_Calendar_ID,
                                            Verb_ID = CalendarItemsOO.Verb_ID,
                                            Entry_ID = Entry_ID_Param == -1 ? CalendarItemsOO.Entry_ID : Entry_ID_Param,
                                            Entry_Name = CalendarItemsOO.Entry_Name,
                                            Status_ID = CalendarItemsOO.Status_ID,
                                            StatusNote = CalendarItemsOO.StatusNote,
                                            isNoDate = CalendarItemsOO.isNoDate,
                                            isAllDay = CalendarItemsOO.isAllDay,
                                            isNote = CalendarItemsOO.isNote,
                                            isRollover = CalendarItemsOO.isRollover,
                                            isSpan = CalendarItemsOO.isSpan,
                                            isEmailConfirmRequired = CalendarItemsOO.isEmailConfirmRequired,
                                            Start_Date = CalendarItemsOO.Start_Date,
                                            End_Date = CalendarItemsOO.End_Date,
                                            Create_Date = CalendarItemsOO.Create_Date,
                                            Update_Date = CalendarItemsOO.Update_Date,
                                            Created_User_ID = CalendarItemsOO.Created_User_ID,
                                            Updated_User_ID = CalendarItemsOO.Updated_User_ID,
                                            GUID = CalendarItemsOO.GUID,
                                            Salutation = CalendarItemsOO.Salutation,
                                            FirstName = CalendarItemsOO.FirstName,
                                            MiddleName = CalendarItemsOO.MiddleName,
                                            LastName = CalendarItemsOO.LastName
                                        };
                var TempCalendarItem = TempCalendarItems.First();

                NewCalendarItem = new CalendarItem()
                    {
                        CalendarItem_ID = TempCalendarItem.CalendarItem_ID,
                        CalendarItem_Calendar_ID = TempCalendarItem.CalendarItem_Calendar_ID,
                        Verb_ID = TempCalendarItem.Verb_ID,
                        Entry_ID = Entry_ID_Param == -1 ? TempCalendarItem.Entry_ID : Entry_ID_Param,
                        Entry_Name = TempCalendarItem.Entry_Name,
                        Status_ID = TempCalendarItem.Status_ID,
                        StatusNote = TempCalendarItem.StatusNote,
                        isNoDate = TempCalendarItem.isNoDate,
                        isAllDay = TempCalendarItem.isAllDay,
                        isNote = TempCalendarItem.isNote,
                        isRollover = TempCalendarItem.isRollover,
                        isSpan = TempCalendarItem.isSpan,
                        isEmailConfirmRequired = TempCalendarItem.isEmailConfirmRequired,
                        Start_Date = TempCalendarItem.Start_Date,
                        End_Date = TempCalendarItem.End_Date,
                        Create_Date = TempCalendarItem.Create_Date,
                        Update_Date = TempCalendarItem.Update_Date,
                        Created_User_ID = TempCalendarItem.Created_User_ID,
                        Updated_User_ID = TempCalendarItem.Updated_User_ID,
                        GUID = TempCalendarItem.GUID,
                        Salutation = TempCalendarItem.Salutation,
                        FirstName = TempCalendarItem.FirstName,
                        MiddleName = TempCalendarItem.MiddleName,
                        LastName = TempCalendarItem.LastName
                    };
            }
            ObservableCollection<CalendarItem> NewCalendarItems = new ObservableCollection<CalendarItem>();
            NewCalendarItems.Add(NewCalendarItem);

            var vw_Appointment_Detail_from_Linq = from CalendarItemOO in NewCalendarItems
                                                  join EntryO in context.Entries on CalendarItemOO.Entry_ID equals EntryO.Entry_ID 
                                                  join Entry_FieldO in context.Entry_Field on CalendarItemOO.Entry_ID equals Entry_FieldO.Entry_ID
                                                  join FieldO in context.Fields on Entry_FieldO.Field_ID equals FieldO.Field_ID
                                                  join VerbO in context.Verbs on CalendarItemOO.Verb_ID equals VerbO.Verb_ID                                                  
                                                  select new
                                                      {
                                                          CalendarItemOO.CalendarItem_ID,
                                                          CalendarItemOO.Salutation,
                                                          CalendarItemOO.FirstName,
                                                          CalendarItemOO.MiddleName,
                                                          CalendarItemOO.LastName,
                                                          CalendarItemOO.CalendarItem_Calendar_ID,
                                                          EntryO.Entry_ID,
                                                          FieldO.Field_ID,
                                                          FieldO.Field1,
                                                          Value = string.Empty,
                                                          VerbO.Verb_ID,
                                                          VerbO.Verb1,
                                                          EntryO.Entry1,
                                                          CalendarItemOO.StatusNote,
                                                          CalendarItemOO.Start_Date,
                                                          CalendarItemOO.End_Date,
                                                          CalendarItemOO.Status_ID
                                                      };
      
            var TempCalendarItem_Field = from CalendarItem_FieldO in context.CalendarItem_Field
                                         where (CalendarItem_FieldO.CalendarItem_ID == CalendarItem_ID_Param)
                                         select CalendarItem_FieldO;
            
            ObservableCollection<i_vw_Appointment_Detail> Newvw_Appointment_Details = new ObservableCollection<i_vw_Appointment_Detail>();

            foreach (var Curvvw_Appointment_Detail_from_Linq in vw_Appointment_Detail_from_Linq)
            {
                string CurFieldValue = Curvvw_Appointment_Detail_from_Linq.Value;
                foreach (var CurTempCalendarItem_Field in TempCalendarItem_Field)
                    if (CurTempCalendarItem_Field.Field_ID == Curvvw_Appointment_Detail_from_Linq.Field_ID)
                    {
                        CurFieldValue = CurTempCalendarItem_Field.Value;
                        break;
                    }
                
                Newvw_Appointment_Details.Add(new i_vw_Appointment_Detail()
                    {
                        vw_Appointment_Detail_ID = Utils.ReplaceNULL,
                        CalendarItem_ID = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.CalendarItem_ID),
                        Salutation = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Salutation),
                        FirstName = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.FirstName),
                        MiddleName = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.MiddleName),
                        LastName = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.LastName),
                        CalendarItem_Calendar_ID = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.CalendarItem_Calendar_ID),
                        Entry_ID = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Entry_ID),
                        Field_ID = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Field_ID),
                        Field = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Field1),
                        Value = Utils.IsNull(CurFieldValue),
                        Verb_ID = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Verb_ID),
                        Verb = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Verb1),
                        Entry = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Entry1),
                        StatusNote = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.StatusNote),
                        Start_Date = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Start_Date),
                        End_Date = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.End_Date),
                        Status_ID = Utils.IsNull(Curvvw_Appointment_Detail_from_Linq.Status_ID),
                    });
            }
            byte[] ResultBinary = SilverlightSerializer.Serialize(Newvw_Appointment_Details);
            return ResultBinary;
        }//public byte[] i_Getvw_Appointment_Detail_in_Binary_via_CustomFunction()



        /*
        [WebInvoke(Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/SayHello")]
        public string SayHello(string name)
        {
        
            return "ktuktu";
        }
        */

        


    }//public class i7s : DataService<i7e>
        
    
}//namespace EDMD
